﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Company.Shell.Interfaces;
using Company.Shell.Models;
using Company.Shell.Services;
using Company.Shell.Views;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using System.Windows.Input;

namespace Company.Shell.ViewModels
{
    internal class MainViewModel : ObservableObject
    {
        private object? content;
        /// <summary>
        /// 子页面
        /// </summary>
        public object? Content
        {
            get { return content; }
            set { SetProperty(ref content, value); }
        }

        public ICommand? OpenViewCommand { get; }
        public ICommand? ExitCommand { get; }
        public ISession Session { get; }

        public MainViewModel(ISession session)
        {
            Session = session;
            ExitCommand = new RelayCommand(OnExitCommand);
            OpenViewCommand = new RelayCommand<string>(OnOpenViewCommand);
            Content = App.Current.Services.GetService<IndexView>();
        }

        /// <summary>
        /// 退出系统
        /// </summary>
        private void OnExitCommand()
        {
            var result = HandyControl.Controls.MessageBox.Show("是否退出系统", "温馨提示", System.Windows.MessageBoxButton.YesNo);
            if(result == System.Windows.MessageBoxResult.Yes)
            {
                WeakReferenceMessenger.Default.Send(new ExitSystemMessage("退出系统"));
            }
        }

        /// <summary>
        /// 切换页面
        /// </summary>
        /// <param name="viewName"></param>
        private void OnOpenViewCommand(string? viewName)
        {
            Assembly assembly = this.GetType().Assembly;
            string fullname = $"{assembly.GetName().Name}.Views.{viewName}";
            Type? type = assembly.GetType(fullname);
            if (type != null)
            {
                Content = App.Current.Services.GetService(type);//导航子页面
            }
        }
    }
}
